package cn.org.donald.framework.controller;


import cn.org.donald.framework.constant.InfoConstant;
import cn.org.donald.framework.constant.TableConstant;
import cn.org.donald.framework.pojo.dto.LoginUser;
import cn.org.donald.framework.pojo.vo.HttpResult;
import cn.org.donald.framework.service.SysMenuService;
import cn.org.donald.framework.service.SysRoleService;
import cn.org.donald.framework.util.RequestUtil;
import cn.org.donald.pojo.entity.system.Role;
import cn.org.donald.framework.base.BaseController;
import cn.org.donald.framework.pojo.dto.PageDto;
import cn.org.donald.framework.pojo.vo.MenuTreeVo;
import cn.org.donald.framework.pojo.vo.TableResult;
import cn.org.donald.framework.util.ListUtil;
import cn.org.donald.pojo.entity.system.Menu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Donald
 * @since 2020-11-09
 */
@RestController
@RequestMapping("/sysMenu")
@Api(value = "菜单管理", description = "菜单管理相关接口")
public class SysMenuController extends BaseController {

    @Reference(version = "1.0.0",group = "donald")
    private SysMenuService sysMenuService;

    @Reference(version = "1.0.0",group = "donald")
    private SysRoleService sysRoleService;

    @PostMapping("/getTree")
    @ApiOperation(value = "获取菜单树形集合")
    @RequiresPermissions("sysMenu:getTree:post")
    public TableResult getTree(@RequestBody PageDto<Menu> pageDto){
        List<MenuTreeVo> menuTreeVos = sysMenuService.findTree(pageDto);
        return toTableResult(menuTreeVos);
    }

    @PostMapping("/add")
    @ApiOperation(value = "获取菜单")
    @RequiresPermissions("sysMenu:add:post")
    public HttpResult add(@RequestBody Menu menu){
        return sysMenuService.save(menu)?toResultSuccess(InfoConstant.ADD_SUCCESS):toResultFail(InfoConstant.ADD_FAIL);
    }


    @GetMapping("/getTreeByRoleId/{id}")
    @ApiOperation(value = "根据角色id获取已分配的菜单")
    @RequiresPermissions("sysMenu:getTreeByRoleId:get")
    public TableResult getTreeByRoleId(@PathVariable Long id){
        List<MenuTreeVo> list = sysMenuService.findTreeByRoleIds(id);
        return toTableResult(list);
    }

    @PostMapping("/getTreeByUser")
    @ApiOperation(value = "根据用户信息获取已分配的菜单")
    public TableResult getTreeByUserId(HttpServletRequest request){
        LoginUser loginUser = RequestUtil.getLoginUser(request);
        List<Role> roleList = sysRoleService.findByUserId(loginUser.getUserId());
        if (!ListUtil.isEmptyOrHasClassFieldAllEmpty(roleList)){
            for (Role role : roleList) {
                if (TableConstant.VALUE_ADMIN_NAME.equals(role.getRoleName())){
                    PageDto<Menu> pageDto = new PageDto<>();
                    List<MenuTreeVo> tree = sysMenuService.findTree(pageDto);
                    return toTableResult(tree);
                }
            }
            Long[] ids = new Long[roleList.size()];
            for(int i =0; i<roleList.size();i++){
                ids[i] = roleList.get(i).getId();
            }
            List<MenuTreeVo> list = sysMenuService.findTreeByRoleIds(ids);
            return toTableResult(list);
        }
        return toTableResult(new ArrayList());

    }

    @GetMapping("/getTreeByNotRoleId")
    @ApiOperation(value = "根据角色id获取未分配的菜单")
    @RequiresPermissions("sysMenu:getTreeByNotRoleId:get")
    public TableResult getTreeByNotRoleId(Long id){
        List<MenuTreeVo> list = sysMenuService.findTreeByNotRoleId(id);
        return toTableResult(list);
    }

    @PostMapping("/getParentList")
    @ApiOperation(value = "获取顶级菜单")
    @RequiresPermissions("sysMenu:getParentList:post")
    public TableResult getParentList(){
        List<Menu> list = sysMenuService.getParentList();
        return toTableResult(list);
    }

    @PostMapping("/getByParentId")
    @ApiOperation(value = "根据parentId获取菜单")
    @RequiresPermissions("sysMenu:getByParentId:post")
    public TableResult getByParentId(Long id){
        List<Menu> list = sysMenuService.findByParentId(id);
        return toTableResult(list);
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除菜单")
    @RequiresPermissions("sysMenu:delete:post")
    public HttpResult delete(@PathVariable Long id){
        //查询是否含有子菜单
        List<Menu> list = sysMenuService.findByParentId(id);
        if (!ListUtil.isEmptyOrHasClassFieldAllEmpty(list)){
            return toResultFail("该菜单下还有子菜单,请先删除或修改子菜单");
        }
        return sysMenuService.removeById(id)?toResultSuccess(InfoConstant.DELETE_SUCCESS):toResultFail(InfoConstant.DELETE_FAIL);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑菜单")
    @RequiresPermissions("sysMenu:edit:put")
    public HttpResult edit(@RequestBody Menu menu){
        if (menu.getId().equals(menu.getParentId()))toResultFail("不能用自身做父菜单");
        return sysMenuService.updateById(menu)?toResultSuccess(InfoConstant.EDIT_SUCCESS):toResultFail(InfoConstant.EDIT_FAIL);
    }

}

